home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 441 / dlibs12 / putenv.c < prev    next >
C/C++ Source or Header  |  1990-11-23  |  2KB  |  75 lines

  1. #include <stdio.h>
  2. #include <basepage.h>
  3.  
  4. extern char    *_envp;
  5.  
  6. #define    ENVSIZ    (1<<12)            /* 4K environment string */
  7.  
  8. static    int    envset = FALSE;        /* local env created? */
  9.  
  10. int putenv(entry)
  11.     char *entry;
  12. /*
  13.  *    Add <entry> to the environment.  <entry> can be any of the following
  14.  *    forms:
  15.  *        <VARIABLE>        remove <VARIABLE>
  16.  *        <VARIABLE>=        set <VARIABLE> to a null value
  17.  *        <VARIABLE>=<value>    set <VARIABLE> to <value>
  18.  */
  19.     {
  20.     register char *p, *q, *t, c;
  21.     register int len;
  22.     char *malloc(), *getenv();
  23.  
  24.     if(!envset)            /* no local env */
  25.         {
  26.         if((p = malloc(ENVSIZ)) == NULL)
  27.             return(FALSE);
  28.         q = _envp;
  29.         _envp = p;
  30.         envset = TRUE;
  31.         if(q)
  32.             {
  33.             while(*q)
  34.                 while(*p++ = *q++)
  35.                     ;
  36.             }
  37.         else
  38.             *p++ = '\0';
  39.         *p++ = '\0';
  40.         *p = 0xFF;
  41.         }
  42.     for(t=entry; (c = *t) && (c != '='); ++t)
  43.         ;
  44.     *t = '\0';
  45.     if(p = getenv(entry))        /* remove old var */
  46.         {
  47.         q = p;
  48.         while(*q++)            /* find end of old val */
  49.             ;
  50.         p -= (len = strlen(entry));
  51.         while(strncmp(--p, entry, len))    /* find start of old var */
  52.             ;
  53.         while(*q)            /* copy environment tail */
  54.             while(*p++ = *q++)
  55.                 ;
  56.         *p++ = '\0';            /* tie off environment */
  57.         *p = 0xFF;
  58.         }
  59.     if(c == '=')                    /* install new var */
  60.         {
  61.         p = _envp;
  62.         while(*p)        /* find end of env */
  63.             while(*p++)
  64.                 ;
  65.         *t = c;
  66.         q = entry;
  67.         while(*p++ = *q++)        /* copy new entry */
  68.             ;
  69.         *p++ = '\0';            /* tie off environment */
  70.         *p = 0xFF;
  71.         }
  72.     _base->p_env = _envp;        /* update basepage pointer */
  73.     return(TRUE);
  74.     }
  75.